﻿
//#region 节点

//节点构造函数
/*activityType：节点类型
position：节点坐标
canInfo：节点所在画布
isload：判断当前构造函数调用是在打开流程加载节点时还是在绘图添加节点时，默认值undefined（绘图添加节点）
    如果isload为undefined（绘图添加节点），且activityType 为 ActivityType.Merging时，将标记当前节点对应的合并节点
*/
var Activity = function (activityType, position, canInfo, isload) {
    this.activityType = activityType;

    this.guid = Guid.NewGuid();
    this.ActivityProperty = {};
    this.ActivityProperty.guid = this.guid;

    this.ActivityProperty.ENABLE_RETURN = 0;
    this.ActivityProperty.ENABLE_RETURN_BY_PREVIOUS_PATH = 0;
    this.ActivityProperty.ENABLE_TRANSIT = 0;



    this.ActivityProperty.IS_ARTIFICIAL_DECISION = 0;
    this.ActivityProperty.IS_AUTOMATIC = 0;
    this.ActivityProperty.ENABLE_REMEMBER_DELIVER_PATH = 0;
    this.ActivityProperty.ENABLE_DELETE_SUB_FLOW = 0;
    this.ActivityProperty.FINISH_TYPE = 0;
    this.ActivityProperty.SUB_FLOW_TYPE = 0;
    this.ActivityProperty.FINISH_NUMBER = 0;
    this.ActivityProperty.activityType = activityType;
    this.ActivityProperty.isDel = 0;
    this.isDrawRuleStart = false;
    this.isDrawRule = false;

    this.position = position;
    this.Canvas = canInfo.canvas;
    this.Picture = canInfo.cxt;
    this.capture = {
        'iscapture': false,
        'point': new Point(0, 0)
    };



    switch (activityType) {
        case ActivityType.Start:
            this.ActivityName = "开始";
            break;
        case ActivityType.Drafting:
            this.ActivityName = "起草";
            break;
        case ActivityType.Approval:
            this.ActivityName = "审批";
            break;
        case ActivityType.Freedom:
            this.ActivityName = "自由";
            break;
        case ActivityType.Branching:
            this.ActivityName = "分支";
            break;
        case ActivityType.Merging:
            this.ActivityName = "合并";
            break;
        case ActivityType.Subprocesses:
            this.ActivityName = "子流程";
            break;
        case ActivityType.Condition:
            this.ActivityName = "条件";
            break;
        case ActivityType.End:
            this.ActivityName = "结束";
            break;
    }
    this.ClearModel = {};
    this.InitialClearModel();
    this.FromRule = new Array();
    this.ToRule = new Array();
    this.status = 2;
    this.isSelect = 2;
    activityHelper.Activitys[activityHelper.Activitys.length] = this;
    activityHelper.Activitys[activityHelper.Activitys.length - 1].ResetActivity();
    this.BranchingControl = null;
    this.MergingControl = null;
    this.ActivityProperty.activityName = this.ActivityName;
    if (isload == undefined && activityType == ActivityType.Merging) {

        this.BranchingControl = activityHelper.Activitys[activityHelper.Activitys.length - 2];
        activityHelper.Activitys[activityHelper.Activitys.length - 2].MergingControl = this;
    }


}

Activity.prototype = {
    //更换ID
    /*应用场景：当打开一个流程，并在该流程的基础上编辑流程，并将编辑后的流程保存为一个全新的流程时，
    需要对原来流程中节点、规则等对象更换ID*/
    ChangeID: function () {

        var me = this;
        var tempActivityID = me.guid;
        me.guid = Guid.NewGuid();
        this.ActivityProperty.guid = me.guid;
        //RETURN
        if (me.ActivityProperty.WF_RULE != undefined) {
            $.each(me.ActivityProperty.WF_RULE, function (i, returnDto) {
                returnDto.id = Guid.NewGuid();
                returnDto.fromGuid = me.guid;
            });
            $.each(activityHelper.Activitys, function (i, val) {
                if (val.ActivityProperty.WF_RULE != undefined && val.guid != me.guid) {
                    $.each(val.ActivityProperty.WF_RULE, function (j, returnDto) {
                        if (returnDto.toGuid == tempActivityID)
                            returnDto.toGuid = me.guid;
                    })
                }
            });
        }
        //PARTICIPANT
        if (me.ActivityProperty.activityParticipants != undefined) {
            $.each(me.ActivityProperty.activityParticipants, function (i, val) {
                val.guid= me.guid;
            });
        }
    },
    //设置节点的属性
    /*打开流程时，将节点的属性加载到系统中
    property：从数据库中读出的节点实体
    */
    SetProperty: function (property) {
        var me = this;
        this.ActivityProperty = property;
        if (property.guid != undefined)
            this.guid = property.guid;

        this.ActivityName = property.activityName
        //针对导入操作，如果模版中角色不存在当前系统中，则删除已选的角色
        if (me.ActivityProperty != undefined && me.ActivityProperty.activityParticipants != undefined) {
            var totalLength = me.ActivityProperty.activityParticipants.length - 1;
            for (h = totalLength; h >= 0 ; h--) {
                var isRemove = me.ActivityProperty.activityParticipants[h].type == 0;
                if (isRemove) {
                    $.each(SqlHelper.JobList, function (i, val) {
                        {
                            if (val.id == me.ActivityProperty.activityParticipants[h].participantId) {
                                isRemove = false;
                                return false;
                            }
                        }
                    });
                    if (isRemove)
                        me.ActivityProperty.activityParticipants.splice(h, 1);
                }
            }
        }

    },


    //在屏幕上绘制输出当前节点
    /*position：节点位置
    strokeStyle：边线样式
    fillStyle：填充样式
    */
    Draw: function (position, strokeStyle, fillStyle) {

        var activity = this;
        var cxt = this.Picture;
        activity.clearActivity();
        cxt.setTransform(1, 0, 0, 1, position.X, position.Y);
        cxt.strokeStyle = strokeStyle;
        switch (activity.activityType) {
            case ActivityType.Approval:
                {
                    activity.DrawRect(cxt, position, StyleDefine.Activity.Approval.activityRectVertexX, StyleDefine.Activity.Approval.activityRectVertexY, StyleDefine.Activity.Approval.activityRectWidth, StyleDefine.Activity.Approval.activityRectHeight, StyleDefine.Activity.Approval.activityRectAngle, StyleDefine.Activity.Approval.imgWidth, StyleDefine.Activity.Approval.imgHeight, strokeStyle, fillStyle, 'Approval');
                } break;
            case ActivityType.Drafting:
                {
                    activity.DrawRect(cxt, position, StyleDefine.Activity.Drafting.activityRectVertexX, StyleDefine.Activity.Drafting.activityRectVertexY, StyleDefine.Activity.Drafting.activityRectWidth, StyleDefine.Activity.Drafting.activityRectHeight, StyleDefine.Activity.Drafting.activityRectAngle, StyleDefine.Activity.Drafting.imgWidth, StyleDefine.Activity.Drafting.imgHeight, strokeStyle, fillStyle, 'Drafting');
                } break;
            case ActivityType.Condition:
                activity.DrawDiamond(cxt, position, strokeStyle, fillStyle, StyleDefine.Activity.Condition.diamondWidth, StyleDefine.Activity.Condition.diamondHeight);
                break;
            case ActivityType.Branching:
                activity.DrawDiamond(cxt, position, strokeStyle, fillStyle, StyleDefine.Activity.Branching.diamondWidth, StyleDefine.Activity.Branching.diamondHeight);
                break;
            case ActivityType.Merging:
                activity.DrawDiamond(cxt, position, strokeStyle, fillStyle, StyleDefine.Activity.Merging.diamondWidth, StyleDefine.Activity.Merging.diamondHeight);
                break;
            case ActivityType.Start:
                {
                    var grd = cxt.createRadialGradient(-5, -5, 2, 0, 0, StyleDefine.Activity.Start.Radius);
                    grd.addColorStop(0, "white");
                    grd.addColorStop(1, "green");
                    activity.DrawCircle(cxt, StyleDefine.Activity.Start.Radius, grd);
                }
                break;
            case ActivityType.End:
                var grd = cxt.createRadialGradient(-5, -5, 2, 0, 0, StyleDefine.Activity.End.Radius);
                grd.addColorStop(0, "white");
                grd.addColorStop(1, "red");

                this.DrawCircle(cxt, StyleDefine.Activity.End.Radius, grd);
                break;
            case ActivityType.Freedom:
                this.DrawFreedom(StyleDefine.Activity.End.Radius);
                //this.DrawCircle(cxt, StyleDefine.Activity.End.Radius/2, 'rgba(225,0,0,255)');
                break;
        }
    },

    //清除图形的范围
    /*canvas重绘节点（比如拖动节点）时，需要先清除画布上原有图形，该方法用于定义清除画布上清除图像的范围*/
    InitialClearModel: function () {
        var me = this;
        me.ClearModel.offset = 2;
        switch (me.activityType) {
            case ActivityType.Start:
                me.ClearModel.startPointX = -StyleDefine.Activity.Start.Radius - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Start.Radius - me.ClearModel.offset;
                me.ClearModel.width = me.ClearModel.heigth = 2 * StyleDefine.Activity.Start.Radius;
                break;
            case ActivityType.Drafting:
                me.ClearModel.startPointX = -StyleDefine.Activity.Drafting.activityRectVertexX - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Drafting.activityRectVertexY - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Drafting.activityRectVertexX;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Drafting.activityRectVertexY;
                break;
            case ActivityType.Approval:
                me.ClearModel.startPointX = -StyleDefine.Activity.Approval.activityRectVertexX - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Approval.activityRectVertexY - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Approval.activityRectVertexX;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Approval.activityRectVertexY;
                break;
            case ActivityType.Freedom:
                me.ClearModel.startPointX = -StyleDefine.Activity.Start.Radius - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Start.Radius - me.ClearModel.offset;
                me.ClearModel.width = me.ClearModel.heigth = 2 * StyleDefine.Activity.Start.Radius;
                break;
            case ActivityType.Branching:
                me.ClearModel.startPointX = -StyleDefine.Activity.Branching.diamondWidth - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Branching.diamondHeight - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Branching.diamondWidth;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Branching.diamondHeight;
                break;
            case ActivityType.Merging:
                me.ClearModel.startPointX = -StyleDefine.Activity.Merging.diamondWidth - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Merging.diamondHeight - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Merging.diamondWidth;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Merging.diamondHeight;
                break;
            case ActivityType.Subprocesses:
                me.ClearModel.startPointX = -StyleDefine.Activity.Merging.diamondWidth - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Merging.diamondHeight - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Merging.diamondWidth;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Merging.diamondHeight;
                break;
            case ActivityType.Condition:
                me.ClearModel.startPointX = -StyleDefine.Activity.Condition.diamondWidth - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.Condition.diamondHeight - me.ClearModel.offset;
                me.ClearModel.width = 2 * StyleDefine.Activity.Condition.diamondWidth;
                me.ClearModel.heigth = 2 * StyleDefine.Activity.Condition.diamondHeight;
                break;
            case ActivityType.End:
                me.ClearModel.startPointX = -StyleDefine.Activity.End.Radius - me.ClearModel.offset;
                me.ClearModel.startPointY = -StyleDefine.Activity.End.Radius - me.ClearModel.offset;
                me.ClearModel.width = me.ClearModel.heigth = 2 * StyleDefine.Activity.End.Radius;
                break;
        }
        me.ClearModel.startPointX -= 10;
        me.ClearModel.startPointY -= 10;
        me.ClearModel.width += 20;
        me.ClearModel.heigth += 20;

    },
    //清除图形
    /*清除画布上InitialClearModel中定义的范围*/
    clearActivity: function () {
        var me = this;
        var cxt = me.Picture;
        cxt.clearRect(me.ClearModel.startPointX, me.ClearModel.startPointY, me.ClearModel.width + me.ClearModel.offset * 2, me.ClearModel.heigth + me.ClearModel.offset * 2);
        if (me.captureX != null && me.captureX.iscapture)
            cxt.clearRect(me.captureX.point.X - me.ClearModel.width / 2 - me.ClearModel.offset - StyleDefine.Activity.capturelength,
                0,
              me.ClearModel.width + 2 * me.ClearModel.offset + 2 * StyleDefine.Activity.capturelength,
                 StyleDefine.Window.height);
        if (me.captureY != null && me.captureY.iscapture)
            cxt.clearRect(0,
                me.captureY.point.Y - me.ClearModel.heigth / 2 - me.ClearModel.offset - StyleDefine.Activity.capturelength,
                StyleDefine.Window.width,
             me.ClearModel.heigth + 2 * me.ClearModel.offset + 2 * StyleDefine.Activity.capturelength);
        cxt.beginPath();
    },

    //绘制自由节点
    /*自由节点在流程图中用一个空心的圆圈表示
    r：圆圈的半径
    */
    DrawFreedom: function (r) {
        var me = this;
        var ctx = me.Picture;
        ctx.beginPath();
        ctx.arc(0, 0, r, 0, Math.PI * 2, true);
        ctx.closePath();
        ctx.stroke();

    },

    //设定节点的背景色
    /*tmp：指定的默认值
    y:节点高度/2
    */
    generateBackground: function (tmp, y) {
        var me = this;
        var fillStyle;

        if (tmp == undefined) {
            switch (me.isSelect) {

                case 0:
                    fillStyle = 'rgba(255,255,255,1)';
                    break;
                case 1:
                    var grd = me.Picture.createLinearGradient(0, -y, 0, y);
                    grd.addColorStop(0, "#e2eaf5");
                    grd.addColorStop(0.1, "#d9e6f8");
                    grd.addColorStop(0.1, "#cbdff7");
                    grd.addColorStop(0.21, "#daf2fd");
                    grd.addColorStop(0.21, "#bfdbff");
                    grd.addColorStop(0.21, "#b0cff7");
                    grd.addColorStop(0.1, "#dbe8f9");
                    grd.addColorStop(0.2, "#bfdbff");
                    grd.addColorStop(1, "#bfdbff");
                    fillStyle = grd;
                    break;
                case 2:
                    var grd = me.Picture.createLinearGradient(0, -y, 0, y);
                    grd.addColorStop(0, "#e2eaf5");
                    grd.addColorStop(0.1, "#d9e6f8");
                    grd.addColorStop(0.1, "#cbdff7");
                    grd.addColorStop(0.21, "#daf2fd");
                    grd.addColorStop(0.21, "#bfdbff");
                    grd.addColorStop(0.21, "#b0cff7");
                    grd.addColorStop(0.1, "#dbe8f9");
                    grd.addColorStop(0.2, "#bfdbff");
                    grd.addColorStop(1, "#bfdbff");
                    fillStyle = grd;
                    break;
                case 3:
                    var grd = me.Picture.createLinearGradient(0, -y, 0, y);
                    grd.addColorStop(0, "#fdcc91");
                    grd.addColorStop(0.5, "#fdb967");
                    grd.addColorStop(1, "#fbf76a");
                    fillStyle = grd;
                    break;

            }
        }
        else fillStyle = tmp;

        return fillStyle;

    },

    //绘制菱形
    /*分支节点、合并节点、条件节点用菱形表示，当绘制这几种图形时，调用此方法
    cxt：绘图对象
    position：中心位置
    strokeStyle：边线样式
    fillStyle：填充样式
    width：中心点到节点的距离
    height：中心点到节点的距离
    img：未用。。。。。。。
    */
    DrawDiamond: function (cxt, position, strokeStyle, fillStyle, width, height, img) {
        var me = this;
        me.DrawRuleStart();
        cxt.beginPath();
        cxt.save();
        cxt.shadowColor = "rgba(180,180,180,90)";
        //cxt.shadowOffsetX = 4;
        //cxt.shadowOffsetY = 4;
        //cxt.shadowBlur = 3;
        cxt.moveTo(-width, 0);
        cxt.lineTo(0, -height);
        cxt.lineTo(width, 0);
        cxt.lineTo(0, height);
        cxt.closePath();

        cxt.fillStyle = me.generateBackground(undefined, height);


        cxt.shadowOffsetX = 1;
        cxt.shadowOffsetY = 1;
        cxt.shadowBlur = 10;


        cxt.fill();
        cxt.restore();
        if (me.isDrawRuleStart)
            me.DrawRuleStart();
        if (me.activityType == ActivityType.Merging || me.activityType == ActivityType.Branching) {
            cxt.moveTo(-width + 5, 0);
            cxt.lineTo(width - 5, 0);
            if (me.activityType == ActivityType.Branching) {
                cxt.moveTo(0, -height + 5);
                cxt.lineTo(0, height - 5);
            }
        }
        else if (me.activityType == ActivityType.Condition) {
            if (fillStyle == null) fillStyle = 'rgba(0,0,0,1)';
            cxt.fillStyle = fillStyle;
            cxt.textAlign = "center";
            cxt.textBaseline = "middle";
            cxt.font = " 14px Microsoft YaHei";
            cxt.fillText(me.ActivityName,0, 0);
        }
        cxt.stroke();

    },


    //绘制矩形
    /*起草节点、人工活动 使用矩形作为 表示图元
      cxt：绘图对象
    position：中心位置
    strokeStyle：边线样式
    fillStyle：填充样式
    width：中心点到节点的距离
    height：中心点到节点的距离
    img：未用。。。。。。。
    */
    DrawRect: function (cxt, position, vertexX, vertexY, width, height, angle, imgWidth, imgHeight, strokeStyle, fillStyle, img) {
        var me = this;
        var rect = cxt.roundRect(-vertexX, -vertexY, width, height, angle, true);
        cxt.fillStyle = me.generateBackground(undefined, height);
        rect.fill();
        cxt.restore();
        if (me.isDrawRuleStart)
            me.DrawRuleStart();
        var init = -vertexX + 25;
        var step = 15;
        cxt.fillStyle = fillStyle;
        cxt.font = " 14px Microsoft YaHei";
        cxt.textAlign = "center";
        cxt.textBaseline = "middle";
        var i = 0;
        var size = cxt.measureText(me.ActivityName);
        //一行
        if (size.width < 2 * vertexX - 10) {
            cxt.fillText(me.ActivityName, 0, 0);
        }
            //两行
        else if (size.width < 4 * vertexX - 20) {
            while (cxt.measureText(me.ActivityName.substring(0, i)).width <= size.width/2) {
                ++i;
            }
            cxt.fillText(me.ActivityName.substring(0, i - 1), 0,-8);
            cxt.fillText(me.ActivityName.substring(i - 1, me.ActivityName.length ),0,8);

        }//缩小文字间距
        else {
            while (cxt.measureText(me.ActivityName.substring(0, i)).width <= size.width/2) {
                ++i;
            }
            cxt.fillText(me.ActivityName.substring(0, i - 1), 0, -8);
            cxt.fillText(me.ActivityName.substring(i - 1, me.ActivityName.length), 0, 8);
        }

        rect = cxt.roundRect(-vertexX, -vertexY, width, height, angle);
        rect.strokeStyle = strokeStyle;
        rect.stroke();

    },

    DrawStatus: function (status, coordinate, vertexY) {

        var cxt = this.Picture;
        var fillStyle = status == 1 ? '#00ff00' : '#ff0000';
        cxt.DrawCircle(coordinate, vertexY / 2, 5, fillStyle, false);

    },

    DrawCircle: function (cxt, r, style) {
        cxt.DrawCircle(0, 0, r, style, undefined, true);
    },


    ChangeSelectStatus: function (e) {
        var me = this;
        if (me.status != me.isSelect || me.isDrawRuleStart != me.isDrawRule) {
            me.status = me.isSelect;
            me.isDrawRule = me.isDrawRuleStart
            me.ResetActivity();
            if (me.isSelect < 2)
                me.Canvas.css("z-index", "0");
        }
    },

    MoveActivity: function (e, istrans) {
        var me = this;
        if (istrans == undefined)
            e = mathHelper.Move(this.startMovePoint, this.currentActivityPosition, e);
        me.position = e;
        this.Draw(e, "rgba(0,0,0,1)", "rgba(0,0,0,1)");
        $.each(me.FromRule, function (i, val) {
            val.ReSetStartPoint(e);
        });
        $.each(me.ToRule, function (i, val) {
            val.ReSetEndPoint(e);
        });
    },

    SelectActivity: function (e, distinct) {

        var me = this;
        var tmpPoint = canvasHelper.GetPoint(e);
        me.Canvas.css("z-index", "50");
        //设置当前操作节点
        currentActivity = me;
        //设置当前节点样式
        me.ChangeSelectStatus(2, tmpPoint);
        //鼠标位置距离中心的距离，如果小于10，则更改当前选择操作为增加规则，否则仅仅选中当前节点
        if (me.CanHaveFromRule() && me.activityType != ActivityType.End && distinct < 10) {
            gcommand = Command.AddRules;
            var canInfo = canvasHelper.InitialCanvas();
            var rule = new Rule(me);
            currentRule = rule;
        }
        else {
            gcommand = Command.SelectActivity;
            moveActivity = true;
            this.startMovePoint = tmpPoint;
            this.currentActivityPosition = me.position;

            if (this.activityType == ActivityType.Branching) {
                this.MergingControl.ChangeSelectStatus(2, tmpPoint);
            }
            else if (this.activityType == ActivityType.Merging) {
                this.BranchingControl.ChangeSelectStatus(2, tmpPoint);

            }
            $("#divWorkspace").css("cursor", "move");
        }
    },

    ResetActivity: function () {
        var style = "rgba(0,0,0,1)";
        this.Draw(this.position, style, style);
    },

    DrawRuleStart: function () {
        if (this.activityType != ActivityType.Start && this.activityType != ActivityType.End)
            this.DrawCircle(this.Picture, StyleDefine.Rule.SelectRadius, 'rgba(225,0,0,0.5)');
    },

    AddFromRule: function (rule) {
        this.FromRule[this.FromRule.length] = rule;
    },

    AddToRule: function (rule) {
        this.ToRule[this.ToRule.length] = rule;
    },

    CanHaveFromRule: function () {
        if (this.activityType == ActivityType.Condition || this.activityType == ActivityType.Branching || this.activityType == ActivityType.Merging)
            return true;
        else if (this.activityType == ActivityType.End || this.activityType == ActivityType.Freedom) return false;
        else {
            if (this.FromRule.length == 0) return true;
            else {
                var tmp = true;
                $.each(this.FromRule, function (i, val) {
                    if (val.endActivity.activityType != ActivityType.Freedom) {
                        tmp = false;
                        return false;
                    }
                });
                return tmp;
            }
        }
    },

    CanHaveToRule: function () {
        var tmpAc = currentRule.startActivity;
        var tmpFlag = true;
        for (m = 0; m < tmpAc.FromRule.length; m++)
            if (tmpAc.FromRule[m].endActivity.guid == this.guid)
                tmpFlag = false;
        return tmpFlag && this.activityType != ActivityType.Start;
    },

    DrawFocus: function (point) {
        var cxt = this.Picture;

        cxt.beginPath();
        cxt.setTransform(1, 0, 0, 1, point.X, point.Y);
        cxt.strokeStyle = "rgba(225,0,0,1)";
        cxt.moveTo(-5, -5);
        cxt.lineTo(5, 5);
        cxt.moveTo(5, -5);
        cxt.lineTo(-5, 5);
        cxt.stroke();
    },

    DrawReferenceLine: function (currentPosition) {
        var me = this;

        var captureX = canvasHelper.GetCaptureActivity('IsCaptureX', me.position, me);
        var captureY = canvasHelper.GetCaptureActivity('IsCaptureY', me.position, me);
        var xd = false;
        var yd = false;


        this.captureX = captureX;
        this.captureY = captureY;

        var cxt = me.Picture;
        cxt.beginPath();
        if (captureX != null && captureX.iscapture) {
            cxt.DrawLine(new Point(captureX.point.X, 0), new Point(captureX.point.X, StyleDefine.Window.height), StyleDefine.Activity.referenceLineStyle);
            canvasHelper.DrawFocusInvoke(me.Picture, captureX.activity);
            xd = true;
        }
        if (captureY != null && captureY.iscapture) {
            cxt.DrawLine(new Point(0, captureY.point.Y), new Point(StyleDefine.Window.width, captureY.point.Y), StyleDefine.Activity.referenceLineStyle);
            canvasHelper.DrawFocusInvoke(me.Picture, captureY.activity);
            yd = true;

        }

        //存在交叉点,在交叉点出绘制
        if (xd || yd) {
            var point;
            if (xd && yd) {
                point = new Point(captureX.point.X, captureY.point.Y);
                canvasHelper.DrawMovePoint(me.Picture, point, StyleDefine.Activity.capturelength, StyleDefine.Activity.referenceLineStyle);
            }
            else if (xd) {
                point = new Point(captureX.point.X, me.position.Y);
                canvasHelper.DrawMovePoint(me.Picture, point, StyleDefine.Activity.capturelength, StyleDefine.Activity.referenceLineStyle);
            }
            else if (yd) {
                point = new Point(me.position.X, captureY.point.Y);
                canvasHelper.DrawMovePoint(me.Picture, point, StyleDefine.Activity.capturelength, StyleDefine.Activity.referenceLineStyle);
            }
            this.capture = {
                'iscapture': true,
                'point': point
            }
        }
        else {
            this.capture.iscapture = false;
        }
    },


    GetActivityTypeName: function () {
        for (var i in ActivityType) {
            if (ActivityType[i] == this.activityType)
                return i;
        }
        return "Start"

    },

    //显示当前节点的属性窗口
    ShowProperty: function () {
        var me = this;
        var property = Property.InitailProperty(me);

        var layerNum = $.layer({
            type: 1,
            title: false,
            move: [$("#divProptitle"), true],
            fix: false,
            closeBtn: false,
            shadeClose: true,
            shade: [0.5, '#000', true],
            border: [5, 0.3, '#666', true],
            offset: ['50%', '50%'],
            area: ['690px', '390px'],
            page: { dom: property },
            success: function () {
            }
        });
        $(".xubox_main").css("background-color", "transparent");
        $("#btnActivityPropOK").unbind();
        $("#btnActivityPropCancel").unbind();
        $("#btnActivityPropOK").click(function () {
            Property.SaveData(me);
            layer.close(layerNum);
        });
        $("#btnActivityPropCancel").click(function () { layer.close(layerNum); });

    },


    //释放当前节点
    /*
    force  是否强制注销，默认值undefined
    */
    Dispose: function (force) {
        var me = this;
        if (me && (force != undefined || (this.activityType != ActivityType.Start && this.activityType != ActivityType.End && this.activityType != ActivityType.Drafting))) {
            this.Canvas.remove();
            this.Canvas = null;
            activityHelper.Activitys.Remove(this);
            for (j = this.FromRule.length - 1; j >= 0; j--) {
                this.FromRule[j].Dispose(force);
            }
            for (j = this.ToRule.length - 1; j >= 0; j--) {
                this.ToRule[j].Dispose(force);
            }
            if (force == undefined) {
                if (this.activityType == ActivityType.Branching) {

                    this.MergingControl.Dispose('1');
                }
                else if (this.activityType == ActivityType.Merging) {
                    this.BranchingControl.Dispose('1');
                }
            }
            me = undefined;
        } else {
            window.alert("此活动不能删除");
        }
    },

    //保存数据时验证节点设计的是否有问题
    /*
    draftingActivity 起草节点
    endActivity  结束节点
    */
    Validate: function (draftingActivity, endActivity) {
        var me = this;
        if (me.activityType == ActivityType.Start || me.activityType == ActivityType.End)
            return {
                result: true,
                message: ''
            };
        var result = me.PreValidate(activityHelper.DraftingActivity);
        if (result.result) {
            result = me.AfterValidate(activityHelper.EndActivity.Preactivity);
            if (result.result) {
                if (me.activityType == ActivityType.Approval || me.activityType == ActivityType.Drafting) {
                    if (me.ActivityProperty.activityParticipants == undefined || me.ActivityProperty.activityParticipants.length == 0) {
                        result.result = false;
                        result.errorMessage = "[" + this.ActivityProperty.activityName + "]-->必须选人\r\n";
                    }
                }
            }
        }
        return result;
    },

    //验证当前节点是否有到开始节点的路径
    /*draftingActivity起草节点*/
    PreValidate: function (draftingActivity) {

        var me = this;
        if (me.guid == draftingActivity.guid)
            return {
                result: true,
                errorMessage: ''
            };
        var flag = true;
        var errorMessage = "";
        if (this.ToRule.length == 0) {
            errorMessage = "[" + me.ActivityProperty.activityName + "]-->无前驱节点\r\n";
            flag = false;
        }
        if (flag) {
            //  me.GetReturnActivitys(true, me.guid);
            if (me.Preactivity == undefined || !me.Preactivity.contains(draftingActivity)) {
                errorMessage = "[" + this.ActivityProperty.activityName + "]-->无法进入节点\r\n";
                flag = false;
            }
        }
        return {
            result: flag,
            errorMessage: errorMessage
        };
    },

    //验证当前节点是否有到结束节点的路径
    /*endActivity结束节点*/
    AfterValidate: function (endActivity) {


        var flag = true;
        var me = this;
        var errorMessage = '';
        if (me.activityType == ActivityType.Freedom)
            return {
                result: flag,
                errorMessage: errorMessage
            };
        if (me.FromRule.length == 0 && me.activityType != ActivityType.Freedom && me.activityType != ActivityType.Subprocesses) {
            errorMessage = "[" + this.ActivityProperty.activityName + "]-->无后继节点\r\n";
            flag = false;
        }
        else if (!endActivity.contains(this)) {

            errorMessage = "[" + this.ActivityProperty.activityName + "]-->无法结束节点\r\n";
            flag = false;
        }
        return {
            result: flag,
            errorMessage: errorMessage
        };
    }



}
//#endregion

